package com.systemic.controller;

import com.systemic.core.ResponseResult;
import com.systemic.dto.AuthUserDto;
import com.systemic.pojo.User;
import com.systemic.service.AuthService;
import com.systemic.service.UserService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.net.URISyntaxException;


/**
 * @ClassName: RegisterController
 * @Description:
 * @Author:
 * @CreateTime: 2023-12-26 20:08
 * @Version:
 **/

@RestController
@RequestMapping("/register")
@RequiredArgsConstructor
public class RegisterController {

    private final UserService userService;
    private final AuthService authService;

    /**
     * 注册流程：
     * 1.判断邮箱是否可以注册
     * 2.发送验证码至邮箱
     * 3.核对验证码，执行新增用户操作
     */
    // step1.检查邮箱是否已注册
    @PostMapping("/email")
    public ResponseResult<String> findByEmail(@RequestBody User user) {
        System.out.println("校验用户邮箱是否可以注册...");
        String email = user.getAccount();
        User userEntity = userService.findByAccount(email);
        if (userEntity == null) {
            authService.sendMailCode(email);
            return ResponseResult.success("验证码已发送");
        }
        return ResponseResult.failed("该邮箱已注册");
    }

    // step2.检查验证码
    @PostMapping("/verify")
    public ResponseResult<String> verify(@RequestBody AuthUserDto authUserDto) {
        if (authService.register(authUserDto)){
            return ResponseResult.success("验证成功");
        } else {
            return ResponseResult.failed("验证失败");
        }
    }



    // step3.处理用户信息
    @PostMapping("/information")
    public ResponseResult<String> add(@RequestBody User user) {
        System.out.println("接收注册信息...");
        String account = user.getAccount();
        String password = user.getPassword();
        // 判空条件
        if (!account.isEmpty() && !password.isEmpty()) {
            authService.information(user);
            return ResponseResult.success("success");
        }
        return ResponseResult.failed("failed");
    }





}
